package de.stalinski.util;

import de.stalinski.db.ConsumersConnector;
import java.sql.SQLException;

public class UserCreator {

    public String generateUser(String fn, String sn) throws ClassNotFoundException, SQLException {
        String user = "";
        String firstPart = fn.substring(0, 1);
        String lastPart = sn.substring(0, 2);
        user = firstPart + lastPart;
        user = user.toUpperCase();
        int PIN = Crypter.generatePIN();
        boolean inList = new ConsumersConnector().alreadyExists(user);
        if (!inList) {
            return user;
        } else {
            firstPart = fn.substring(0, 1);
            for (int i = 0; i < sn.length() - 2; i++) {
                lastPart = sn.substring(i, i+2);
                user = firstPart + lastPart;
                user = user.toUpperCase();
                inList = new ConsumersConnector().alreadyExists(user);
                if (!inList) {
                    return user;
                }
            }
        }
        return user+PIN;
    }
}
